package com.api.ui
{
	import api.dataStruct.GameApiInfo;
	import api.dataStruct.ShowMouseObj;
	
	import com.api.StaticData;
	import com.api.mc.Again;
	import com.api.mc.AlertMain;
	import com.api.mc.ChatMc;
	import com.api.mc.ChooseUser;
	import com.api.mc.DebugTxt;
	import com.api.mc.FaceList;
	import com.api.mc.InitClew;
	import com.api.mc.MatchPlayer;
	import com.api.mc.Recommend;
	import com.api.mc.RecommendBtn;
	import com.api.mc.StartGame;
	import com.api.mc.TurnOther;
	import com.api.mc.UserLeave;
	import com.api.mc.WaitData;
	import com.game.btnClass.BtnClass_goto;
	import com.game.btnClass.McBtnClass1;
	import com.game.utils.GlobalFun;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class API_UI
	{
		
		//主容器
		private var _main:Sprite;
		private var _bg:Sprite;
		private var _bgSP:Sprite;
		private var _alartSP:Sprite
		private var _floatSP:Sprite
		
		
		//显示对象
		private var _initClew:InitClew
		private var _turnOther:TurnOther;
		private var _chooseUser:ChooseUser;
		private var _dubugTxt:DebugTxt;
		private var _userLeave:UserLeave;
		private var _again:Again;
		private var _alert:AlertMain;
		
		
		
		private var _alertFun:Function;
		public var chatMc:ChatMc;
		private var _startGame:StartGame;
		private var _matchPlayer:MatchPlayer;
		private var _waitData:WaitData;
		private var _recommend:Recommend;
		private var _recommendBtn:RecommendBtn;
		private var _faceList:FaceList;
		
		
		//数据
		private var _isChat:Boolean=false;
		public function API_UI(main:Sprite)
		{
			
			_main=main;
			init();
		}
		private function init():void{
			_bgSP=new Sprite();
			_alartSP=new Sprite();;
			_floatSP=new Sprite();
			_main.addChild(_bgSP);
			_main.addChild(_alartSP);
			_main.addChild(_floatSP);
		}
		public function set isChat(value:Boolean):void
		{
			_isChat = value;
			if(_isChat){
				chatMc=new ChatMc();
				_main.addChild(chatMc);
			}
			_main.addChild(_floatSP);
		}
		public function disFaceList(point:Point,callBack:Function):void{
			delFaceList();
			if(_faceList==null){
				_faceList=new FaceList(callBack,delFaceList);
			}
			_faceList.x=point.x;
			_faceList.y=point.y;
			point=null;
			mainAddDis(_faceList,0,0);
		}
		public function delFaceList():void{
			if(_faceList==null){
				return;
			}
			_faceList.gc();
			mainDelDis(_faceList);
			_faceList=null;
		}
		public function isDisRecommendBtn(type:uint):void{
			delRecommendBtn();
			if(type==0){
				return;
			}
			if (_recommendBtn == null)
			{
				_recommendBtn = new RecommendBtn(type);
				new BtnClass_goto(_recommendBtn,disRecommend);
			}
			_floatSP.addChild(_recommendBtn);
		}
		private function delRecommendBtn():void{
			if (_recommendBtn == null)
			{
				return;
			}
			_floatSP.removeChild(_recommendBtn);
			_recommendBtn = null;
		}
		public function disRecommend() : void
		{
			if (_recommend == null)
			{
				_recommend = new Recommend();
				_recommend.init(StaticData.config.recommend, delRecommend);
			}
			mainAddDis(_recommend);
		}
		
		private function delRecommend() : void
		{
			if (_recommend == null)
			{
				return;
			}
			_recommend.gc();
			mainDelDis(_recommend);
			_recommend = null;
		}
		public function disWaitData(point:Point=null):void{
			delWaitData();
			var _isCenter:uint=0;
			if(_waitData==null){
				_waitData=new WaitData();
			}
			if(point==null){
				_isCenter=1;
			}else{
				_waitData.x=point.x;
				_waitData.y=point.y;
			}
			
			mainAddDis(_waitData,0,_isCenter);
		}
		public function delWaitData():void{
			
			if(_waitData==null){
				return;
			}
			mainDelDis(_waitData,0);
			_waitData=null;
		} 
		
		public function disMatchPlayer(fun:Function):void{
			delMatchPlayer();
			if(_matchPlayer==null){
				_matchPlayer=new MatchPlayer(fun);
			}
			mainAddDis(_matchPlayer);
		}
		
		public function delMatchPlayer():void{
			if(_matchPlayer==null){
				return;
			}
			
			mainDelDis(_matchPlayer);
			_matchPlayer.gc();
			_matchPlayer=null;
		}
		public function disStartGame(fun:Function):void{
			delStartGame();
			if(_startGame==null){
				_startGame=new StartGame(fun);
			}
			mainAddDis(_startGame);
		}
		public function delStartGame():void{
			if(_startGame==null){
				return;
			}
			mainDelDis(_startGame);
			_startGame=null;
		}
		
		public function initChat(sendFun:Function):void{
			if(chatMc!=null){
				chatMc.initChat(sendFun);
				chatMc.y=StaticData.gameH-StaticData.chatHeight;
			}
		}
		
		
		public function disAlert(str:String,fun:Function=null):void
		{
			delAlert();
			if(_alert==null){
				_alert=new AlertMain(str,delAlert);
			}
			_alertFun=fun
			mainAddDis(_alert);
		}
		public function delAlert():void
		{
			if(_alertFun!=null){
				_alertFun();
			}
			if(_alert==null){
				return;
				
			}
			_alert.gc();
			mainDelDis(_alert);
			_alert=null;
			
		}
		public function disAgain(againFun:Function,changeFun:Function):void{
			delAgain();
			if(_again==null){
				_again=new Again(againFun,changeFun);
			}
			mainAddDis(_again);
		}
		public function delAgain():void{
			if(_again==null){
				return;
			}
			mainDelDis(_again);
			_again.gc();
			_again=null;
			
		}
		public function disUserLeave(fun:Function):void{
			
			delUserLeave();
			if(_userLeave==null){
				_userLeave=new UserLeave(fun);
			}
			mainAddDis(_userLeave);
		}
		public function delUserLeave():void{
			if(_userLeave==null){
				return;
			}
			_userLeave.gc();
			mainDelDis(_userLeave);
			_userLeave=null;
		}
		public function disDebugTxt(str:String):void{
			if(_dubugTxt==null){
				_dubugTxt=new DebugTxt();
			}
			_dubugTxt.addStr(str);
			mainAddDis(_dubugTxt,0,0)
			
		}
		public function delDebugTxt():void{
			if(_dubugTxt==null){
				return;
			}
			mainDelDis(_dubugTxt,0);
		}
		public function disInitClew(str:String,clew:String=""):void{
			delInitClew()
			if(_initClew==null){
				_initClew=new InitClew();
			}
			
			_initClew.setClew(str,clew);
			mainAddDis(_initClew)
			
		}
		public function delInitClew():void{
			if(_initClew==null){
				return;
			}
			mainDelDis(_initClew);
			_initClew=null;
		}
		public function disChooseUser(fun:Function):void{
			if(_chooseUser==null){
				_chooseUser=new ChooseUser();
			}
			_chooseUser.init(fun);
			mainAddDis(_chooseUser)
			
		}
		public function delChooseUser():void{
			if(_chooseUser==null){
				return;
			}
			_chooseUser.gc();
			mainDelDis(_chooseUser);
			_chooseUser=null;
		}
		public function isDisTurnMe(isDis:Boolean):void{
			delTurnOther();
			if(isDis){
				return;
			}
			if(_turnOther==null){
				_turnOther=new TurnOther();
			}
			mainAddDis(_turnOther,0)
		}
		private  function delTurnOther():void{
			if(_turnOther==null){
				return;
			}
			mainDelDis(_turnOther,0);
			_turnOther=null;
		}
		
		
		
		
		
		
		
		
		
		
		
		//====================================================
		/**
		 * 添加显示对象
		 * @param disObj  显示对象
		 * @param isHaveBg 是否具有背景,0:无,1:有
		 * @param isCenter 是否居中,0:不居中,1:居中
		 * */
		private function mainAddDis(disObj:DisplayObjectContainer,isHaveBg:uint=1,isCenter:uint=1):void{
			//isCenter=1居中,isCenter!=0,不居中
			if(isHaveBg==0){
				isDelBg();
			}else{
				isAddBg();
			}
			if(!_alartSP.contains(disObj)){
				
				_alartSP.addChild(disObj);
			}else{
				trace("API_UI.mainAddDis,disObj已经存在",disObj);
				return;
			}
			if(isCenter==1){
				setCenter(disObj);
			}
		}
		private function mainDelDis(disObj:DisplayObjectContainer,isHaveBg:uint=1):void
		{
			if(_alartSP.contains(disObj)){
				_alartSP.removeChild(disObj);
				
			}else{
			}
			if(isHaveBg==1){
				isDelBg();
			}
		}
		private function isAddBg():void{
			if(_bg!=null||_bgSP==null){
				return;
			}
			_bg=new Sprite();
			_bg.graphics.beginFill(0x000000,0.2);
			_bg.graphics.drawRect(0,0,StaticData.gameW,StaticData.gameH);
			_bgSP.addChild(_bg);
		}
		private function isDelBg():void{
			if(_bg==null){
				return;
			}
			if(_bgSP.contains(_bg)){
				_bgSP.removeChild(_bg);
				_bg=null;
			}
		}
		public function delAllAlert():void{
			GlobalFun.removeAllDisObj(_alartSP);
			isDelBg();
		}
		private function setCenter(disObj:DisplayObjectContainer):void{
			var _gW:uint=StaticData.gameW;
			var _gH:uint=StaticData.gameH;
			if(_gW==0){
				_gW=_main.stage.width
			}
			if(_gH==0){
				_gH=_main.stage.height;
			}
			disObj.x=(_gW-disObj.width)/2;
			disObj.y=((_gH-StaticData.chatHeight)-disObj.height-1)/2;
		}
	}
}